package com.bojiu.lawsearch.lawsearch.controller;

import com.bojiu.lawsearch.lawsearch.form.AppWithdrawForm;
import com.bojiu.lawsearch.lawsearch.model.Task;
import com.bojiu.lawsearch.lawsearch.service.WithdrawService;
import com.bojiu.lawsearch.lawsearch.util.AuthorizationHandle;
import com.bojiu.lawsearch.lawsearch.util.JsonResult;
import com.bojiu.lawsearch.lawsearch.util.ResultCode;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

/**
 * @author: afdiao
 * @Date：2020/4/14 15:33
 * @Description:
 */
@Api(description = "用户提现管理")
@RestController
@RequestMapping("withdraw")
public class WithdrawController {

    @Autowired
    AuthorizationHandle authorizationHandle;

    @Autowired
    private WithdrawService withdrawService;
    /**
     * 申请提现
     *
     * @param request
     * @return
     */
    @ApiOperation(value = "申请提现", notes = "提现至微信")
    @ApiResponses({
            @ApiResponse(code = 200, message = "成功", response = Task.class),
            @ApiResponse(code = 600, message = "未检测到登录，跳转重新登录"),
            @ApiResponse(code = 500, message = "没有权限访问该接口")
    })
    @PostMapping("apply")
    public JsonResult apply(HttpServletRequest request, @Valid @RequestBody AppWithdrawForm form) {
        //先判断权限
        JsonResult authon = authorizationHandle.webHandler(request);
        if (!authon.getCode().equals("200")) {
            return authon;
        }

        withdrawService.apply(Integer.parseInt(authon.getData().toString()), form.getAmount());
        return new JsonResult(ResultCode.SUCCESS);
    }
}
